import React from 'react';
import {Spin, Card, Button, Row, Col, Table, Form, Popconfirm, Message as msg} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {FORM_OPTIONS, TABLE, FIXED_COLUMN_WIDTH, FORM_ROW_OPTIONS} from '../constants';
import {addApplyReport, getFinally} from './api';
import {generateRowSpanData} from './utils';
import {vehicleFollowListDestinationPort} from '../Enum';
import TextInput from 'Shared/components/TextInput';
import WrappedDatePicker from '../common/WrappedDatePicker';
import {injectIntl, language} from './intl';
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
import WrappedSelect from '../common/WrappedSelect';
import InputAmount from '../common/InputAmount';
import WrappedInputNumber from '../common/WrappedInputNumber';
import ImportPanel from './ImportPanel';
import styles from './style.css';
import Return from '../common/Return';
import {newOrUpdateOrderValidator} from './validator';
const FormItem = Form.Item;

const vehicleFollowListDestinationPortToList = vehicleFollowListDestinationPort.toList(language);
class AddPanel extends React.PureComponent {
    state = {
        loading: false,
        hasOperat: false,
        isValidate: false,
        showImportPanel: false,
        data: {},
        details: [],
    }
    componentWillUnmount() {
        this.isunmount = true;
    }
    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };
    onChange = (value, name) => {
        const data = Object.assign({}, this.state.data, {
            [name]: value
        });
        this.setState({
            data,
            hasOperat: true
        });
    }
    onClickOkOnPopconfirm = () => {
        this.props.onCancel();
    }
    onSubmit = () => {
        const data = this.state.data;
        const details = this.state.details;
        const isValid = newOrUpdateOrderValidator(data, details);
        if(!isValid) {
            this.setState({
                isValidate: true
            });
            return;
        }
        const condition = {
            dealerId: data.dealerId,
            contactInfo: data.contactInfo,
            saleConfirmationrCode: data.saleConfirmationrCode,
            from: data.from,
            to: data.to,
            deliveryTerms: data.deliveryTerms,
            consignee: data.consignee,
            buyer: data.buyer,
            frameTradeTerms: data.frameTradeTerms,
            vehicleContractNo: data.vehicleContractNo,
            vehicleContractDate: data.vehicleContractDate,
            frameContractNo: data.frameContractNo,
            contractDate: data.contractDate,
            payment: data.payment,
            tradeMethod: data.tradeMethod,
            detail: details.map(v => ({
                invoiceCode: v.invoiceCode,
                invoiceDate: v.invoiceDate,
                containerCode: v.containerCode,
                containerDate: v.containerDate,
                leadMark: v.leadMark,
                frameCode: v.frameCode,
                emptyContainerWeight: v.emptyContainerWeight,
                bindingBands: v.bindingBands,
                carriageWeight: v.carriageWeight,
                container: v.container,
                cheryCar: v.cheryCar,
                version: v.version,
                color: v.color,
                perGW: v.perGW,
                glonassNO: v.glonassNO,
                engineNumber: v.engineNumber,
                displacement: v.displacement,
                hsCode: v.hsCode,
                vin: v.vin,
                productionYear: v.productionYear,
                ottc: v.ottc,
                qty: v.qty,
                unitPriceExw: v.unitPriceExw,
                insurance: v.insurance,
                freight1: v.freight1,
                freight2: v.freight2,
                totalAmount: v.totalAmount,
                vehicleDescribe: v.vehicleDescribe,
                mileage: v.mileage,
                manufactureDate: v.manufactureDate,
            })),
        };
        this.setState({loading: true});
        addApplyReport(condition).then(res => {
            if(res.ok)
                this.props.successReturn();
            if(!this.isunmount)
                this.setState({loading: false});
        });
    }
    onDeleteAll = () => {
        this.setState({
            details: [],
            hasOperat: true
        });
    }
    onDeleteDetail = e => {
        const id = e.target.dataset.id;
        const details = this.state.details.filter(p => p.id !== id);
        this.setState({
            details,
            hasOperat: true
        });
    }
    onDetailonChange = (value, name, id) => {
        const details = this.state.details || [];
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    [name]: value ? value : ' ',
                });
            return d;
        });
        this.setState({
            details: tmp,
            hasOperat: true
        });
    };
    onDetailonChangePerGW = (value, record) => {
        const details = this.state.details || [];
        const Weight = this.state.details.filter(v => v.id !== record.id);
        let threeVINWeight = 0;
        
        const tmp = details.map(d => {
            if(d.id === record.id)
                return Object.assign({}, d, {
                    perGW: value ? value : ' ',
                });
            return d;
        });
        const invoice = tmp.filter(v => v.invoiceCodeBak === record.invoiceCodeBak);
        if(invoice)
            invoice.forEach(d => {
                threeVINWeight += (d.perGW);
            });
        const tmp1 = tmp.map(d => {
            if(d.invoiceCodeBak === record.invoiceCodeBak)
                return Object.assign({}, d, {
                    threeVINWeight,
                    totalWeight: threeVINWeight + (Weight.emptyContainerWeight || 0)
                });
            return d;
        });
        this.setState({
            details: tmp1,
            hasOperat: true
        });
    };
    onSelectDealer = (condition = {}) => {
        const id = condition.id;
        this.setState({loading: true});
        getFinally(id)
            .then(res => {
                if(res.ok && res.data.id)
                    this.setState({
                        loading: false,
                        data: res.data,
                    });
                else {
                    this.setState({
                        loading: false,
                        hasOperat: true,
                        data: {
                            dealerId: condition.id,
                            dealerCode: condition.code,
                            dealerName: condition.name,
                        }
                    });
                    return msg.warn(this.props.intl.formatMessage({
                        id: 'addPanel.message.noVehiceleFollow',
                        defaultMessage: '该总代没有相关随车单据'
                    }));
                }
            });
    };
    onClickImportBtn = () => {
        this.setState({showImportPanel: true});
    }
    onImportChange = records => {
        if(records.payload) {
            const importDetails = this.state.details || [];
            records.payload.content.forEach(r => {
                const index = importDetails.findIndex(v => v.id === r.id);
                if(index === -1)
                    importDetails.push({
                        id: r.id,
                        containerDate: r.containerDate,
                        invoiceCode: r.invoiceCode,
                        invoiceDate: r.invoiceDate,
                        containerCode: r.containerCode,
                        leadMark: r.leadMark,
                        frameCode: r.frameCode,
                        emptyContainerWeight: r.emptyContainerWeight,
                        bindingBands: r.bindingBands,
                        container: r.container,
                        cheryCar: r.cheryCar,
                        version: r.version,
                        color: r.color,
                        perGW: r.perGW,
                        glonassNO: r.glonassNO,
                        engineNumber: r.engineNumber,
                        displacement: r.displacement,
                        hsCode: r.hsCode,
                        vin: r.vin,
                        productionYear: r.productionYear,
                        ottc: r.ottc,
                        qty: r.qty,
                        unitPriceExw: r.unitPriceExw,
                        insurance: r.insurance,
                        freight1: r.freight1,
                        freight2: r.freight2,
                        totalAmount: r.totalAmount,
                        vehicleDescribe: r.vehicleDescribe,
                        mileage: r.mileage,
                        manufactureDate: r.manufactureDate,
                        carriageWeight: r.carriageWeight,
                    });
                else
                    importDetails.splice(index, 1, {
                        id: r.id,
                        containerDate: r.containerDate,
                        invoiceCode: r.invoiceCode,
                        invoiceDate: r.invoiceDate,
                        containerCode: r.containerCode,
                        leadMark: r.leadMark,
                        frameCode: r.frameCode,
                        emptyContainerWeight: r.emptyContainerWeight,
                        bindingBands: r.bindingBands,
                        container: r.container,
                        cheryCar: r.cheryCar,
                        version: r.version,
                        color: r.color,
                        perGW: r.perGW,
                        glonassNO: r.glonassNO,
                        engineNumber: r.engineNumber,
                        displacement: r.displacement,
                        hsCode: r.hsCode,
                        vin: r.vin,
                        productionYear: r.productionYear,
                        ottc: r.ottc,
                        qty: r.qty,
                        unitPriceExw: r.unitPriceExw,
                        insurance: r.insurance,
                        freight1: r.freight1,
                        freight2: r.freight2,
                        totalAmount: r.totalAmount,
                        vehicleDescribe: r.vehicleDescribe,
                        mileage: r.mileage,
                        manufactureDate: r.manufactureDate,
                        carriageWeight: r.carriageWeight,
                    });
            });
            this.setState({
                details: [...importDetails],
                hasOperat: true,
                showImportPanel: false,
            });
        }
    }
    onCloseImportPanel = () => {
        this.setState({showImportPanel: false});
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {data, details, isValidate, loading} = this.state;
        const {brands, tradeMethods} = this.props.initEnumData;
        if(brands.length === 1 && !this.props.id) {
            data.brandId = brands[0].value;
            data.brandName = brands[0].text;
        }
        const dealerInfo = data.dealerId ? {
            id: data.dealerId,
            code: data.dealerCode,
            name: data.dealerName
        } : null;
        const newDetails = generateRowSpanData(details, 'vehicleFollowId',
            ['invoiceCode', 'invoiceDate', 'containerCode', 'containerDate',
                'leadMark', 'frameCode', 'emptyContainerWeight', 'threeVINWeight',
                'totalWeight', 'bindingBands', 'carriageWeight']);
        const columns = [{
            title: <span className="ant-form-item-required">{this.props.intl.formatMessage({
                id: 'addPanel.column.invoiceCode',
                defaultMessage: '发票号'
            })}</span>,
            dataIndex: 'invoiceCode',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'invoiceCode' && this.props.sortedOrder,
            render: (text, record, index) => {
                const validate = !text && isValidate ? {validateStatus: 'error'} : null;
                const obj = {
                    children: (
                        <FormItem {...validate}>
                            <TextInput
                                id={record.id}
                                name="invoiceCode"
                                value={text}
                                onBlur={value => this.onDetailonChange(value, 'invoiceCode', record.id)} />
                        </FormItem>),
                    props: {},
                };
                if(index === 0)
                    obj.props.rowSpan = 2;
                if(index === 1)
                    obj.props.rowSpan = 0;
                return obj;
            }
        },
        {
            title: <span className="ant-form-item-required">{this.props.intl.formatMessage({
                id: 'addPanel.column.invoiceDate',
                defaultMessage: '开票日期'
            })}</span>,
            dataIndex: 'invoiceDate',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'invoiceDate' && this.props.sortedOrder,
            render: (text, record, index) => {
                const validate = !text && isValidate ? {validateStatus: 'error'} : null;
                const obj = {
                    children: (
                        <FormItem {...validate}>
                            <WrappedDatePicker
                                id={record.id}
                                name="invoiceDate"
                                value={text}
                                onChange={value => this.onDetailonChange(value, 'invoiceDate', record.id)} />
                        </FormItem>),
                    props: {},
                };
                if(index === 0)
                    obj.props.rowSpan = 2;
                if(index === 1)
                    obj.props.rowSpan = 0;
                return obj;
            }
        },
        {
            title: <span className="ant-form-item-required">{this.props.intl.formatMessage({
                id: 'addPanel.column.containerCode',
                defaultMessage: '箱号'
            })}</span>,
            dataIndex: 'containerCode',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'containerCode' && this.props.sortedOrder,
            render: (text, record, index) => {
                const validate = !text && isValidate ? {validateStatus: 'error'} : null;
                const obj = {
                    children: (
                        <FormItem {...validate}>
                            <TextInput
                                id={record.id}
                                name="containerCode"
                                value={text}
                                onBlur={value => this.onDetailonChange(value, 'containerCode', record.id)} />
                        </FormItem>),
                    props: {},
                };
                if(index === 0)
                    obj.props.rowSpan = 2;
                if(index === 1)
                    obj.props.rowSpan = 0;
                return obj;
            }
        },
        {
            title: <span className="ant-form-item-required">{this.props.intl.formatMessage({
                id: 'addPanel.column.containerDate',
                defaultMessage: '装箱日期'
            })}</span>,
            dataIndex: 'containerDate',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'containerDate' && this.props.sortedOrder,
            render: (text, record, index) => {
                const validate = !text && isValidate ? {validateStatus: 'error'} : null;
                const obj = {
                    children: (
                        <FormItem {...validate}>
                            <WrappedDatePicker
                                id={record.id}
                                name="containerDate"
                                value={text}
                                onChange={value => this.onDetailonChange(value, 'containerDate', record.id)} />
                        </FormItem>),
                    props: {},
                };
                if(index === 0)
                    obj.props.rowSpan = 2;
                if(index === 1)
                    obj.props.rowSpan = 0;
                return obj;
            }
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.leadMark',
                defaultMessage: '铅封号'
            }),
            dataIndex: 'leadMark',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'leadMark' && this.props.sortedOrder,
            render: (text, record, index) => {
                const obj = {
                    children: (
                        <FormItem>
                            <TextInput
                                id={record.id}
                                name="leadMark"
                                value={text}
                                onBlur={value => this.onDetailonChange(value, 'leadMark', record.id)} />
                        </FormItem>),
                    props: {},
                };
                if(index === 0)
                    obj.props.rowSpan = 2;
                if(index === 1)
                    obj.props.rowSpan = 0;
                return obj;
            }
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.frameCode',
                defaultMessage: '铁架子编号'
            }),
            dataIndex: 'frameCode',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'frameCode' && this.props.sortedOrder,
            render: (text, record, index) => {
                const obj = {
                    children: (
                        <FormItem>
                            <TextInput
                                id={record.id}
                                name="frameCode"
                                value={text}
                                onBlur={value => this.onDetailonChange(value, 'frameCode', record.id)} />
                        </FormItem>),
                    props: {},
                };
                if(index === 0)
                    obj.props.rowSpan = 2;
                if(index === 1)
                    obj.props.rowSpan = 0;
                return obj;
            }
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.emptyContainerWeight',
                defaultMessage: '空箱重量(KG)'
            }),
            dataIndex: 'emptyContainerWeight',
            render: (text, record, index) => {
                const obj = {
                    children: (
                        <FormItem>
                            <WrappedInputNumber
                                id={record.id}
                                value={text}
                                onBlur={value => this.onDetailonChange(value, 'emptyContainerWeight', record.id)} />
                        </FormItem>),
                    props: {},
                };
                if(index === 0)
                    obj.props.rowSpan = 2;
                if(index === 1)
                    obj.props.rowSpan = 0;
                return obj;
            }
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.threeVINWeight',
                defaultMessage: '三车重量(KG)'
            }),
            dataIndex: 'threeVINWeight',
            render: (text, record, index) => {
                const obj = {
                    children: text,
                    props: {}
                };
                if(index === 0)
                    obj.props.rowSpan = 2;
                if(index === 1)
                    obj.props.rowSpan = 0;
                return obj;
            }
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.totalWeight',
                defaultMessage: '总重量(KG)'
            }),
            dataIndex: 'totalWeight',
            render: (text, record, index) => {
                const obj = {
                    children: record.emptyContainerWeight + record.threeVINWeight,
                    props: {},
                };
                if(index === 0)
                    obj.props.rowSpan = 2;
                if(index === 1)
                    obj.props.rowSpan = 0;
                return obj;
            }
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.bindingBands',
                defaultMessage: '绑扎带'
            }),
            dataIndex: 'bindingBands',
            render: (text, record, index) => {
                const obj = {
                    children: (
                        <FormItem>
                            <WrappedInputNumber
                                id={record.id}
                                value={text}
                                onBlur={value => this.onDetailonChange(value, 'bindingBands', record.id)} />
                        </FormItem>),
                    props: {},
                };
                if(index === 0)
                    obj.props.rowSpan = 2;
                if(index === 1)
                    obj.props.rowSpan = 0;
                return obj;
            }
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.carriageWeight',
                defaultMessage: '车架子毛重(KG)'
            }),
            dataIndex: 'carriageWeight',
            render: (text, record, index) => {
                const obj = {
                    children: (
                        <FormItem>
                            <WrappedInputNumber
                                id={record.id}
                                value={text}
                                onBlur={value => this.onDetailonChange(value, 'carriageWeight', record.id)} />
                        </FormItem>),
                    props: {},
                };
                if(index === 0)
                    obj.props.rowSpan = 2;
                if(index === 1)
                    obj.props.rowSpan = 0;
                return obj;
            }
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.container',
                defaultMessage: '物料号'
            }),
            dataIndex: 'container',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="container"
                        value={text}
                        onBlur={value => this.onDetailonChange(value, 'container', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.cheryCar',
                defaultMessage: 'CHERY CAR'
            }),
            dataIndex: 'cheryCar',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="cheryCar"
                        value={text}
                        onBlur={value => this.onDetailonChange(value, 'cheryCar', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.version',
                defaultMessage: '车型'
            }),
            dataIndex: 'version',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="version"
                        value={text}
                        onBlur={value => this.onDetailonChange(value, 'version', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.color',
                defaultMessage: '颜色'
            }),
            dataIndex: 'color',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="color"
                        value={text}
                        onBlur={value => this.onDetailonChange(value, 'color', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.perGW',
                defaultMessage: '单车重量(KG)'
            }),
            dataIndex: 'perGW',
            render: (text, record) => (
                <FormItem>
                    <WrappedInputNumber
                        id={record.id}
                        value={text}
                        onBlur={value => this.onDetailonChangePerGW(value, record)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.glonassNO',
                defaultMessage: 'Glonass NO.'
            }),
            dataIndex: 'glonassNO',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="glonassNO"
                        value={text}
                        onBlur={value => this.onDetailonChange(value, 'glonassNO', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.engineNumber',
                defaultMessage: '发动机号'
            }),
            dataIndex: 'engineNumber',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="engineNumber"
                        value={text}
                        onBlur={value => this.onDetailonChange(value, 'engineNumber', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.displacement',
                defaultMessage: '排量'
            }),
            dataIndex: 'displacement',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="displacement"
                        value={text}
                        onBlur={value => this.onDetailonChange(value, 'displacement', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.hsCode',
                defaultMessage: 'HS CODE'
            }),
            dataIndex: 'hsCode',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="hsCode"
                        value={text}
                        onBlur={value => this.onDetailonChange(value, 'hsCode', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.vin',
                defaultMessage: 'VIN'
            }),
            dataIndex: 'vin',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="vin"
                        value={text}
                        onBlur={value => this.onDetailonChange(value, 'vin', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.productionYear',
                defaultMessage: '产品年份'
            }),
            dataIndex: 'productionYear',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="productionYear"
                        value={text}
                        onBlur={value => this.onDetailonChange(value, 'productionYear', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.ottc',
                defaultMessage: 'OTTC'
            }),
            dataIndex: 'ottc',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="ottc"
                        value={text}
                        onBlur={value => this.onDetailonChange(value, 'ottc', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.qty',
                defaultMessage: '数量'
            }),
            dataIndex: 'qty',
            render: (text, record) => (
                <FormItem>
                    <WrappedInputNumber
                        id={record.id}
                        value={text}
                        onBlur={value => this.onDetailonChange(value, 'qty', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.unitPriceExw',
                defaultMessage: '货值'
            }),
            dataIndex: 'unitPriceExw',
            render: (text, record) => (
                <FormItem>
                    <InputAmount
                        id={record.id}
                        min={0}
                        value={text}
                        onChange={value => this.onDetailonChange(value, 'unitPriceExw', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.insurance',
                defaultMessage: '保险费'
            }),
            dataIndex: 'insurance',
            render: (text, record) => (
                <FormItem>
                    <InputAmount
                        id={record.id}
                        min={0}
                        value={text}
                        onChange={value => this.onDetailonChange(value, 'insurance', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.freight1',
                defaultMessage: '运费一'
            }),
            dataIndex: 'freight1',
            render: (text, record) => (
                <FormItem>
                    <InputAmount
                        id={record.id}
                        min={0}
                        value={text}
                        onChange={value => this.onDetailonChange(value, 'freight1', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.freight2',
                defaultMessage: '运费二'
            }),
            dataIndex: 'freight2',
            render: (text, record) => (
                <FormItem>
                    <InputAmount
                        id={record.id}
                        min={0}
                        value={text}
                        onChange={value => this.onDetailonChange(value, 'freight2', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.totalAmount',
                defaultMessage: '总价格'
            }),
            dataIndex: 'totalAmount',
            render: (text, record) => (
                <FormItem>
                    <InputAmount
                        id={record.id}
                        min={0}
                        value={text}
                        onChange={value => this.onDetailonChange(value, 'totalAmount', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.vehicleDescribe',
                defaultMessage: '整车描述'
            }),
            dataIndex: 'vehicleDescribe',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        name="vehicleDescribe"
                        value={text}
                        onBlur={value => this.onDetailonChange(value, 'vehicleDescribe', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.mileage',
                defaultMessage: '公里数(Km)'
            }),
            dataIndex: 'mileage',
            render: (text, record) => (
                <FormItem>
                    <WrappedInputNumber
                        id={record.id}
                        value={text}
                        onBlur={value => this.onDetailonChange(value, 'mileage', record.id)} />
                </FormItem>
            )
        },
        {
            title: this.props.intl.formatMessage({
                id: 'addPanel.column.manufactureDate',
                defaultMessage: '生产日期'
            }),
            dataIndex: 'manufactureDate',
            render: (text, record) => (
                <FormItem>
                    <WrappedDatePicker
                        id={record.id}
                        name="manufactureDate"
                        value={text}
                        onChange={value => this.onDetailonChange(value, 'manufactureDate', record.id)} />
                </FormItem>
            )
        },
        {
            title: details.length > 0
                ? (
                    <Popconfirm placement="topRight" title={formatMessage({
                        id: 'addPanel.validation.isDelete',
                        defaultMessage: '是否确认全部删除？'
                    })} onConfirm={this.onDeleteAll} >
                        <a>{formatMessage({
                            id: 'addPanel.validation.clear',
                            defaultMessage: '清空'
                        })}</a>
                    </Popconfirm>
                )
                : null,
            dataIndex: 'id',
            fixed: 'right',
            width: FIXED_COLUMN_WIDTH,
            render: text => <a data-id={text} onClick={this.onDeleteDetail}>{formatMessage({
                id: 'addPanel.validation.delete',
                defaultMessage: '删除'
            })}</a>
        }];
        return (
            <div className="form-standard">
                <Spin spinning={this.state.loading}>
                    <Card title={formatMessage({
                        id: 'addPanel.column.common',
                        defaultMessage: '随车单据信息'
                    })}>
                        <Form>
                            <Row>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={this.props.intl.formatMessage({
                                        id: 'addPanel.operation.dealerId',
                                        defaultMessage: '总代'
                                    })}{...FORM_OPTIONS.item} validateStatus={isValidate && !data.dealerId ? 'error' : null} required>
                                        <DealerSelectPanel
                                            value={dealerInfo}
                                            onChange={this.onChange}
                                            onSelect={this.onSelectDealer} />
                                    </FormItem>
                                </Col>
                                {brands.length !== 1 && <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.brandCode',
                                        defaultMessage: '品牌'
                                    })} {...FORM_OPTIONS.item}>
                                        {data.brandCode === undefined ? '' : `(${data.brandCode})${data.brandCode}`}
                                    </FormItem>
                                </Col>}
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.saleConfirmationrCode',
                                        defaultMessage: '销确号'
                                    })} {...FORM_OPTIONS.item} validateStatus={isValidate && !data.saleConfirmationrCode ? 'error' : null} required>
                                        <TextInput
                                            name="saleConfirmationrCode"
                                            value={data.saleConfirmationrCode}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.from',
                                        defaultMessage: '起运港'
                                    })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="from"
                                            value={data.from}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.to',
                                        defaultMessage: '目的港'
                                    })} {...FORM_OPTIONS.item}>
                                        <WrappedSelect
                                            allowClear
                                            name="to"
                                            options={vehicleFollowListDestinationPortToList}
                                            value={data.to}
                                            onChange={this.onChange}/>
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.deliveryTerms',
                                        defaultMessage: '贸易条款'
                                    })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="deliveryTerms"
                                            value={data.deliveryTerms}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem className={styles.multiLabel} label={formatMessage({
                                        id: 'addPanel.operation.consignee',
                                        defaultMessage: '整车发票和铁架子发票的收货人'
                                    })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="consignee"
                                            value={data.consignee}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.buyer',
                                        defaultMessage: '铁架子发票的买方'
                                    })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="buyer"
                                            value={data.buyer}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.frameTradeTerms',
                                        defaultMessage: '铁架子贸易条款'
                                    })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="frameTradeTerms"
                                            value={data.frameTradeTerms}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.vehicleContractNo',
                                        defaultMessage: '整车合同号'
                                    })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="vehicleContractNo"
                                            value={data.vehicleContractNo}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.vehicleContractDate',
                                        defaultMessage: '整车合同日期'
                                    })} {...FORM_OPTIONS.item}>
                                        <WrappedDatePicker
                                            name="vehicleContractDate"
                                            value={data.vehicleContractDate}
                                            onChange={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.frameContractNo',
                                        defaultMessage: '铁架子合同号'
                                    })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="frameContractNo"
                                            value={data.frameContractNo}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.contractDate',
                                        defaultMessage: '铁架子合同日期'
                                    })} {...FORM_OPTIONS.item}>
                                        <WrappedDatePicker
                                            name="contractDate"
                                            value={data.contractDate}
                                            onChange={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.payment',
                                        defaultMessage: '付款方式'
                                    })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="payment"
                                            value={data.payment}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.tradeMethod',
                                        defaultMessage: '贸易方式'
                                    })} {...FORM_OPTIONS.item}>
                                        <WrappedSelect
                                            allowClear
                                            name="tradeMethod"
                                            options={tradeMethods}
                                            value={data.tradeMethod}
                                            onChange={this.onChange}/>
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row className="row-margin">
                                <Col>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.contactInfo',
                                        defaultMessage: '联系信息'
                                    })} {...FORM_ROW_OPTIONS.item}>
                                        <TextInput
                                            name="contactInfo"
                                            type="textarea"
                                            value={data.contactInfo}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                            </Row>
                        </Form>
                    </Card>
                    <Card
                        title={formatMessage({
                            id: 'addPanel.operation.detail',
                            defaultMessage: '随车单据清单'
                        })} extra={<div>
                            <Button
                                size="small"
                                onClick={this.onClickImportBtn}>
                                {formatMessage({
                                    id: 'addPanel.operation.import',
                                    defaultMessage: '导入清单'
                                })}
                            </Button>
                        </div>}>
                        <Table
                            className="white-space-nowrap"
                            columns={columns}
                            rowKey="id"
                            onChange={this.onChangeSort}
                            pagination={false}
                            dataSource={newDetails}
                            {...TABLE} />
                    </Card>
                    <Card className={styles.optionBtn}>
                        <Button
                            type="primary"
                            onClick={this.onSubmit}
                            loading={loading}>
                            {formatMessage({
                                id: 'addPanel.operation.onSubmit',
                                defaultMessage: '保存'
                            })}
                        </Button>
                    </Card>
                </Spin>
                {
                    this.state.showImportPanel &&
                    <ImportPanel
                        selectKey={details.id}
                        onCancel={this.onCloseImportPanel}
                        onImportChange={this.onImportChange} />
                }
                <div className="page-toolbar">
                    <Return
                        disabled={this.state.loading}
                        onConfirm={this.onClickOkOnPopconfirm}
                        showWarning={this.state.hasOperat} />
                </div>
            </div>
        );
    }
}

AddPanel.propTypes = {
    successReturn: PropTypes.func.isRequired,
    onCancel: PropTypes.func.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    onSelect: PropTypes.func.isRequired,
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
import {
    tableSearch,
    saveQueryDealer
} from './actions';
import {createSelector} from 'reselect';

const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'init']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: `(${item.code})${item.name}`,
            value: item.id
        })) : [];
        const tradeMethods = initData.tradeMethods ? initData.tradeMethods.map(item => ({
            text: `(${item.code}) ${item.name}`,
            value: item.id
        })) : [];
        return {
            brands,
            tradeMethods,
        };
    }
);
const mapStateToProps = state => ({
    initEnumData: getinitEnumData(state),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder']),
});
const mapDispatchToProps = (dispatch, props) => ({
    onSelect: data => dispatch(saveQueryDealer(data)),
    onChangeSort: option => dispatch(tableSearch(option)),
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(AddPanel));
